package cn.com.qingz.modules.user.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.core.model.DefaultResp;
import cn.com.qingz.core.model.EasyUITree;
import cn.com.qingz.modules.user.model.Category;
import cn.com.qingz.modules.user.service.CategoryService;
import cn.com.qingz.modules.user.vo.CategorySearchCondition;

/**
 * @author codeGen
 */
@Controller
@RequestMapping("category/*")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 保存分类
     * @param
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public DefaultResp<String> save(Category entity) {
        DefaultResp<String> res = new DefaultResp<String>();
        try {
            categoryService.doAddOrUpdate(entity);
        } catch (org.springframework.dao.DuplicateKeyException e) {
            throw new DicmfException(SystemConstantes.ERROR, "分类编码已经存在");
        }
        return res;
    }

	@RequestMapping("delete")
	@ResponseBody
	public DefaultResp<String> delete(Integer categoryid) {
		DefaultResp<String> res = new DefaultResp<String>();
		categoryService.doDelById(categoryid);
		return res;
	}

    @RequestMapping("getCategoryTree")
    @ResponseBody
    public List<EasyUITree> getCategoryTree() {
          EasyUITree root = new EasyUITree("0", "全部");
          return categoryService.findChildrenByParent(root);
    }

    //select * from category where 1=1 order by level asc
    //select count, max(level) from category
//
//    public List<EasyUITree>  findChildNode(EasyUITree root){
//           List<EasyUITree>  children =  mapper.find( root.getId() );
//           if(children.isEmpty()) return ;
//           root.setChildren(children);
//           for (EasyUITree child : children) {
//               findChildNode(child) ;
//           }
//    }



//
//    private List<EasyUITree> resourceToTreeNode(int max,List<Category> resource) {
////        //Use isEmpty() to check whether the collection is empty or not.
////        if (resource != null && !resource.isEmpty() ) {
////            List<EasyUITree> ch = new ArrayList<EasyUITree>();
////            for (Category rr : resource) {
////                EasyUITree node = new EasyUITree();
////                node.setId(rr.getParentId());
////                node.setState("open");
////                node.setText(rr.getTitle());
////                ch.add(node);
////                node.setChildren(resourceToTreeNode(rr.getSubResource()));
////            }
////
////            return ch;
////        }
////        //Empty arrays and collections should be returned instead of null
////
////
////        return Collections.emptyList();
//
//        int loop =0;
//        while (loop != max){
//            loop++;
//            List<node> nodes =  mapper.find(loop,loop+1);
//            for (node node : nodes) {
//                nodeparent nodechild
//            }
//            }
//
//
//        }
//
//
//    }

    /**
     * 分页查询
     * @param condition
     * @param rows
     * @param page 页码
     * @return rows 每页行数
     */
    @RequestMapping("queryForPage")
    @ResponseBody
    public Map<String, Object> queryByPage(CategorySearchCondition condition, @RequestParam(required = false, defaultValue = "0") int page,
            @RequestParam(required = false, defaultValue = "10") int rows) {
        PageList<Category> list = categoryService.queryForPage(condition, page, rows);
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("total", list.getPaginator().getTotalCount());
        res.put("rows", list);
        return res;
    }

}